package ecom.appcode.web.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ecom.appcode.business.CategoryLogic;
import ecom.appcode.business.CategoryLogicImpl;
import ecom.appcode.business.ContactLogic;
import ecom.appcode.business.ContactLogicImpl;
import ecom.appcode.business.ShoesLogic;
import ecom.appcode.business.ShoesLogicImpl;
import ecom.appcode.business.UsersLogic;
import ecom.appcode.business.UsersLogicImpl;
import ecom.appcode.common.constants.CommonConstants;
import ecom.appcode.common.daobean.CategoryDaoBean;
import ecom.appcode.common.daobean.ContactDaoBean;
import ecom.appcode.common.daobean.ShoesDaoBean;
import ecom.appcode.common.daobean.UsersDaoBean;
import ecom.appcode.dao.daointerface.ContactDao;

/**
 * Servlet implementation class AdministratorAction
 */
public class AdministratorAction extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final String product_management = "product-management.jsp";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdministratorAction() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String action = request.getParameter(CommonConstants.PARAM_BUTTON_SUBMIT);

        if ("loadShoes".equals(action)) {
            ShoesLogic shoesLogic = new ShoesLogicImpl();
            List<ShoesDaoBean> shoesList = shoesLogic.loadAllShoes();
            if (shoesList != null) {
                HttpSession session = request.getSession();
                session.setAttribute("LOADALLSHOES", shoesList);
                RequestDispatcher rd = request.getRequestDispatcher(product_management);
                rd.forward(request, response);
            }
        } else if (CommonConstants.BUTTON_DELETE.equals(action)) {
            String email = request.getParameter(CommonConstants.PARAM_EMAIL);

            UsersLogic userLogic = new UsersLogicImpl();
            boolean result = userLogic.deleteAccount(email);

            if (result) {
                UsersLogic usersLogic = new UsersLogicImpl();
                List<UsersDaoBean> usersList = usersLogic.loadAllUsers();
                if (usersList != null) {
                    HttpSession session = request.getSession();
                    session.setAttribute(CommonConstants.LOAD_ALL_USER, usersList);
                    RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.USER_MANAGER_PAGE);
                    rd.forward(request, response);
                }
            }
        } else if (CommonConstants.BUTTON_VIEW_ALL_CATEGORY.equals(action)) {
            /** Load Category */
            CategoryLogic cateLogic = new CategoryLogicImpl();
            List<CategoryDaoBean> cateList = cateLogic.loadCategoryName();
            HttpSession session = request.getSession();
            session.setAttribute(CommonConstants.SESSION_CATEGORY_LIST, cateList);
            RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.CATEGORY_MANAGER_PAGE);
            rd.forward(request, response);

        } else if (CommonConstants.BUTTON_VIEW_CATEGORY_BY_ID.equals(action)) {
            int categoryID = CommonConstants.NUM_0;
            String string_categoryID = request.getParameter(CommonConstants.PARAM_CATEGORY_ID);
            try {
                categoryID = Integer.parseInt(string_categoryID);
            } catch (NumberFormatException ex) {
                // TODO: handle exception
                ex.printStackTrace();
            }

            /** Load Category */
            CategoryLogic cateLogic = new CategoryLogicImpl();
            CategoryDaoBean cateList = cateLogic.loadCategoryByID(categoryID);
            HttpSession session = request.getSession();
            session.setAttribute(CommonConstants.SESSION_CATEGORY_LIST, cateList);
            RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.EDIT_CATEGORY_MANAGER_PAGE);
            rd.forward(request, response);
        } else if (CommonConstants.BUTTON_DELETE_CATEGORY.equals(action)) {
            String string_categoryID = request.getParameter(CommonConstants.PARAM_CATEGORY_ID);
            int categoryID = Integer.parseInt(string_categoryID);

            CategoryLogic category = new CategoryLogicImpl();
            boolean result = category.deleteCategory(categoryID);

            if (result) {
                List<CategoryDaoBean> cateList = category.loadCategoryName();
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.SESSION_CATEGORY_LIST, cateList);
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.CATEGORY_MANAGER_PAGE);
                rd.forward(request, response);
            }
        } else if (CommonConstants.BUTTON_VIEW_ALL_CONTACT.equals(action)) {
            ContactLogic contact = new ContactLogicImpl();
            List<ContactDaoBean> contactList = contact.viewAllContact();
            
            HttpSession session = request.getSession();
            session.setAttribute(CommonConstants.SESSION_CONTACT_LIST, contactList);
            
            RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.CONTACT_MANAGER_PAGE);
            rd.forward(request, response);
        } else if (CommonConstants.BUTTON_DELETE_CONTACT.equals(action)) {
            String string_contactID = request.getParameter(CommonConstants.PARAM_CONTACT_ID);
            int contactID = Integer.parseInt(string_contactID);
            ContactLogic contLogic = new ContactLogicImpl();
            boolean result = contLogic.deleteContact(contactID);
            
            if(result) {
                List<ContactDaoBean> contactList = contLogic.viewAllContact();
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.SESSION_CONTACT_LIST, contactList);
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.CONTACT_MANAGER_PAGE);
                rd.forward(request, response);
            }
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
        String action = request.getParameter(CommonConstants.PARAM_BUTTON_SUBMIT);
        if (CommonConstants.BUTTON_EDIT.equals(action)) {

            String email = request.getParameter(CommonConstants.PARAM_EMAIL);
            String password = request.getParameter(CommonConstants.PARAM_PASSWORD);
            String sex = request.getParameter(CommonConstants.PARAM_SEX);
            // BIRTH DAY
            String day = request.getParameter(CommonConstants.PARAM_SHOES_DAY);
            String month = request.getParameter(CommonConstants.PARAM_SHOES_MONTH);
            String year = request.getParameter(CommonConstants.PARAM_SHOES_YEAR);
            String birthday = month + CommonConstants.SLASH + day + CommonConstants.SLASH + year;

            String firstName = request.getParameter(CommonConstants.PARAM_FIRST_NAME);
            String lastName = request.getParameter(CommonConstants.PARAM_LAST_NAME);
            String admin = request.getParameter(CommonConstants.PARAM_ADMIN);

            String address = request.getParameter(CommonConstants.PARAM_ADDRESS);
            String string_phoneNumber = request.getParameter(CommonConstants.PARAM_PHONE);

            int phoneNumber = Integer.parseInt(string_phoneNumber);
            boolean isAdmin = false;
            if (admin != null) {
                isAdmin = true;
            }
            UsersLogic userLogic = new UsersLogicImpl();
            boolean result = userLogic.updateAccount(email, password, sex, birthday, firstName, lastName, isAdmin,
                    address, phoneNumber);

            if (result) {
                UsersLogic usersLogic = new UsersLogicImpl();
                List<UsersDaoBean> usersList = usersLogic.loadAllUsers();
                if (usersList != null) {
                    HttpSession session = request.getSession();
                    session.setAttribute(CommonConstants.LOAD_ALL_USER, usersList);
                    RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.USER_MANAGER_PAGE);
                    rd.forward(request, response);
                }
            }
        } else if (CommonConstants.BUTTON_EDIT_CATEGORY.equals(action)) {
            String string_categoryID = request.getParameter(CommonConstants.PARAM_CATEGORY_ID);
            int categoryID = Integer.parseInt(string_categoryID);
            String categoryName = request.getParameter(CommonConstants.PARAM_CATEGORY_NAME);
            String description = request.getParameter(CommonConstants.PARAM_CATEGORY_DESCRIPTION);

            CategoryLogic category = new CategoryLogicImpl();
            boolean result = category.updateCategory(categoryID, categoryName, description);
            if (result) {
                List<CategoryDaoBean> cateList = category.loadCategoryName();
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.SESSION_CATEGORY_LIST, cateList);
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.CATEGORY_MANAGER_PAGE);
                rd.forward(request, response);
            }
        } else if (CommonConstants.BUTTON_ADD_CATEGORY.equals(action)) {
            String categoryName = request.getParameter(CommonConstants.PARAM_CATEGORY_NAME);
            String description = request.getParameter(CommonConstants.PARAM_CATEGORY_DESCRIPTION);
            CategoryLogic category = new CategoryLogicImpl();
            boolean result = category.addCategory(categoryName, description);
            if (result) {
                List<CategoryDaoBean> cateList = category.loadCategoryName();
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.SESSION_CATEGORY_LIST, cateList);
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.CATEGORY_MANAGER_PAGE);
                rd.forward(request, response);
            }
        }
    }

}
